<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<div class="bjui-pageHeader" style="background-color:#fefefe; border-bottom:none;">
    <form data-toggle="ajaxsearch" id="cp_form">
        <fieldset>
            <legend style="font-weight:normal;">条件搜索：</legend>
            <div style="margin:0; padding:11px 25px 25px;">

                <label class="row-label">发起方：</label>
                <input type="text" name="userName" class="form-control" size="15" title="发起方">

                <label class="row-label">&nbsp;接收方：</label>
                <input type="text" name="inUserName" class="form-control" size="15" title="接收方">

                <label class="row-label">&nbsp;标号：</label>
                <input type="text" name="loanNumber" class="form-control" size="15" title="标的编号">

                <label class="row-label">&nbsp;流水号：</label>
                <input type="text" name="orderNo" class="form-control" size="15" title="流水号">

                <label class="row-label">&nbsp;交易时间：</label>
                <input type="text" name="from" data-toggle="datepicker" data-nobtn="true" size="11">
                <label class="row-label">-</label>
                <input type="text" name="to" data-toggle="datepicker" data-nobtn="true" size="11">

                <div class="btn-group">
                    <button type="submit" class="btn-green" data-icon="search" onclick="companyAjaxSearch()">搜索</button>
                    <button type="reset" class="btn-orange" data-icon="times">重置</button>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<div class="bjui-pageContent">
    <table id="companyJournal_loan_table" class="table table-bordered"></table>
    <table id="companyJournal_collection_table" class="table table-bordered"></table>
    <table id="companyJournal_repayment_table" class="table table-bordered"></table>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/capital/capital.js"></script>
<script type="text/javascript">
    var companyJournal_loan_table, companyJournal_collection_table, companyJournal_repayment_table;
    $(function () {
        companyJournal_loan_table = $("#companyJournal_loan_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            showToolbar: true,
            filterThead: false,
            gridTitle: '投资记录',
            showTfoot : true,
            delPK: 'id',
            dataUrl: '/admin/capital/loan?userName=company&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData : {total : "companyJournal_purchaseLoan"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="companyExportLoan();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                name: 'userName',
                label: '用户名',
                align: 'center'
            }, {
                name: 'payPlatformID',
                label: '双乾标识',
                align: 'center'
            }, {
                name: 'loanName',
                label: '标名',
                align: 'center'
            }, {
                name: 'loanNumber',
                label: '标号',
                align: 'center'
            }, {
                name: 'amount',
                label: '购买金额',
                align: 'center',
                total:'companyJournal_purchaseLoan'
            }, {
                name: 'date',
                label: '购买时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 19) : value
                }
            }, {
                name: 'status',
                label: '购买状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center'
            }],
            paging: {pageSize: 20, pageIndex: 1},
            linenumberAll: true
        });

        companyJournal_collection_table = $("#companyJournal_collection_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '收款记录',
            showToolbar: true,
            filterThead: false,
            showTfoot : true,
            delPK: 'id',
            dataUrl: '/admin/capital/flow?userName=company&type=5&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData : {total : "companyJournal_collection"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="companyExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                name: 'terminal',
                label: '终端',
                align: 'center'
            }, {
                name: 'amount',
                label: '收款金额',
                align: 'center',
                total : "companyJournal_collection"
            }, {
                name: 'date',
                label: '交易时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 16) : value
                }
            }, {
                render: companyShowUser,
                label: '发起者',
                align: 'center'
            }, {
                name: 'type',
                label: '收款类型',
                align: 'center',
                items: flowType
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center'
            }],
            paging: {pageSize: 20, pageIndex: 1},
            linenumberAll: true
        });

        companyJournal_repayment_table = $("#companyJournal_repayment_table").datagrid({
            height: '50%',
            width: '100%',
            fullGrid : true,
            gridTitle: '还款记录',
            showToolbar: true,
            filterThead: false,
            delPK: 'id',
            dataUrl: '/admin/capital/flow?userName=company&type=2&_=' + new Date().getTime(),
            local: "remote",
            inlineEditMult: false,
            delConfirm: true,
            saveAll: false,
            showTfoot : true,
            addLocation: 'last',
            toolbarItem: 'refresh,',
            postData : {total : "companyJournal_repayment"},
            toolbarCustom: function () {
                return '<button type="button" class="btn btn-green" onclick="companyExport();" data-icon="sign-out"><i class="fa fa-sign-out"></i> 导出</button>';
            },
            columns: [{
                name: 'terminal',
                label: '终端',
                align: 'center'
            }, {
                name: 'amount',
                label: '还款金额',
                align: 'center',
                total : "companyJournal_repayment"
            }, {
                name: 'date',
                label: '交易时间',
                align: 'center',
                pattern: 'yyyy-MM-dd HH:mm:ss',
                render: function (value) {
                    return value ? value.substr(0, 16) : value
                }
            }, {
                render: companyShowOutUser,
                label: '接受者',
                align: 'center'
            }, {
                name: 'status',
                label: '交易状态',
                align: 'center',
                items: flowStatus
            }, {
                name: 'orderNo',
                label: '流水号',
                align: 'center',
                width: 320
            }],
            paging: {pageSize: 20, pageIndex: 1},
            linenumberAll: true
        });

        function companyShowUser(value, data) {
            return '<a href="javascript:;" onclick="companyOpenDialog(\'' + data.userId + "','" + data.userName + '\')">' + data.inUserName + '</a>';
        }

        function companyShowOutUser(value, data) {
            return '<a href="javascript:;" onclick="companyOpenDialog(\'' + data.inUserId + "','" + data.inUserName + '\')">' + data.inUserName + '</a>';
        }

    });

    function companyOpenDialog(id, userName) {
        BJUI.dialog({
            id: 'userInfo_' + id,
            url: '/admin/user/userInfo?id=' + id,
            title: '' + userName + '用户信息',
            width: 1200,
            height: 1200 * 0.618
        });
    }

    function companyExport() {
        var companyCollection = $(companyJournal_collection_table).data('selectedDatas'),
            companyRepayment = $(companyJournal_repayment_table).data('selectedDatas');

        if ((companyCollection && companyCollection.length !== 0)
            || (companyRepayment && companyRepayment.length !== 0)) {
            var ids = [], companyFlows = $.extend(companyCollection, companyRepayment);
            $.each(companyFlows, function (index, value, array) {
                ids.push(value.id);
            });
            BJUI.ajax('ajaxdownload', {
                url: '/admin/capital/exportFlow',
                data: {ids: ids.join(",")}
            });
        } else
            BJUI.alertmsg('info', BJUI.getRegional('datagrid.selectMsg'))
    }
    function companyExportLoan() {
        var dates = $(companyJournal_loan_table).data('selectedDatas');

        if (dates && dates.length !== 0) {
            var ids = [];
            dates.forEach(function (e) {
                ids.push(e.id);
            });
            BJUI.ajax('ajaxdownload', {
                url: '/admin/capital/exportLoan',
                data: {ids: ids.join(",")}
            });
        } else
            BJUI.alertmsg('info', BJUI.getRegional('datagrid.selectMsg'))
    }

    function companyAjaxSearch() {
        var data = $('#cp_form').serializeJson();
        $(companyJournal_loan_table).datagrid('filter', {
            inUserName: data.inUserName,
            orderNo: data.orderNo,
            loanNumber: data.loanNumber,
            'fromTo.from': data.from,
            'fromTo.to': data.to
        });
        $(companyJournal_collection_table).datagrid('filter', {
            inUserName: 'company',
            userName: data.userName,
            orderNo: data.orderNo,
            'fromTo.from': data.from,
            'fromTo.to': data.to
        });
        $(companyJournal_repayment_table).datagrid('filter', {
            userName: 'company',
            inUserName: data.inUserName,
            orderNo: data.orderNo,
            'fromTo.from': data.from,
            'fromTo.to': data.to
        });
    }
</script>